package com.example.administrator.materialdesign;

import android.app.DownloadManager;
import android.app.VoiceInteractor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.dbo.MyDatabasesHelper;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

    public class LoginActivity extends AppCompatActivity {

                private EditText editText_user;
                private EditText editText_psw;
                private Button button_login;
                private Button button_add;
                private Button button_carate;
                private TextView TextView1;

        private MyDatabasesHelper db;
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);





                    
                    setContentView(R.layout.login_activity);

                    editText_user=(EditText)findViewById(R.id.EditText_user);
                    editText_psw=(EditText)findViewById(R.id.EditText_psw);
                    button_login=(Button)findViewById(R.id.Button_login);
                    button_add=(Button)findViewById(R.id.Button_add);
                    button_carate=(Button)findViewById(R.id.Button_carate);
                    TextView1=(TextView) findViewById(R.id.TextView1);

                    db=new MyDatabasesHelper(LoginActivity.this,"test.db",null,1);

                    button_add.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            sendUserOkHttp();
                        }
                    });

                    button_carate.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            db.getWritableDatabase();
                        }
                    });



        button_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendRequsetWithokHttp();
            }
        });

    }


    /*向网络发送数据*/
    private void sendUserOkHttp(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{

                    /*构建一个Resquestbody对象存放待提交的参数*/
                    RequestBody requestBody =new FormBody.Builder().add("userid",editText_user.getText().toString()).add("psw",editText_psw.getText().toString()).build();
                    OkHttpClient client=new OkHttpClient();
                    Request requset=new Request.Builder().url("http://153.0.148.100:40042/android/").post(requestBody).build();
                    Response response=client.newCall(requset).execute();
                    String responseData=response.body().string();
                    showRespones(responseData);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }



    /*像网络上发起请求*/
    private void sendRequsetWithokHttp(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client=new OkHttpClient();
                    Request request=new Request.Builder().url("http://153.0.148.100:40042/android/").build();
                    Response response=client.newCall(request).execute();
                    String responseData=response.body().string();
                    showRespones(responseData);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();

    }

        /*开启一个子线程对ui进行更新*/
        private void showRespones(final String response){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                   TextView1.setText(response);
//                    Toast.makeText(LoginActivity.this, "点击了", Toast.LENGTH_SHORT).show();
                }
            });
        }

}
